package com.tianyu.common;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2024/6/6 - 10:43
 **********************************/

@Data
@Slf4j
@ToString
@Component
@Configuration
@PropertySource(value = "classpath:config/maven.properties", encoding = "utf-8")
@ConfigurationProperties(prefix = "maven")
public class MavenBuildProperties implements CommandLineRunner {

    /**
     * 应用名称
     */
    private String appName;

    /**
     * 应用时间
     */
    private String appVersion;

    /**
     * maven打包时间
     */
    private String packageTime;

    public void setPackageTime(String packageTime) {
        this.packageTime = GlobalTool.modifyTimeToE8(packageTime);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("ProjectBuildInfo: appName=>{}, appVersion=>{}, packageTime=>{}", getAppName(), getAppVersion(), getPackageTime());
        final File file = new File("logs/");
        if (!file.exists() && file.mkdirs()){
            log.info("Create Folder: logs");
        }
        try (final FileOutputStream fos = new FileOutputStream("logs/version.log")) {
            fos.write(JSONObject.from(this).toJSONString(JSONWriter.Feature.PrettyFormat).getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
